﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("YUIAspNet");

YUIAspNet.Tooltip = function(element) {
    YUIAspNet.Tooltip.initializeBase(this, [element]);
}

YUIAspNet.Tooltip.prototype = {
    initialize: function() {
        YUIAspNet.Tooltip.callBaseMethod(this, 'initialize');
       	 
	    //Sys.Application.add_init(Function.createDelegate(this, this.initializeSlider));
    },

    initializeYUIControl: function() {
        var config = this._buildConfig();
        
        this._yuiControl = new YAHOO.widget.Tooltip(this.get_TooltipContainer(), config);
        
        if(this._events)
        {
            var me = this;
            var onMouseOverHandler = this._events.getHandler("OnMouseOver");
            if(onMouseOverHandler)
                this._yuiControl.contextMouseOverEvent.subscribe(function(type, yuiArgs){ 
                    var args = {element: yuiArgs[0], event: yuiArgs[1], cancel: false, yuiargs: yuiArgs};
                    onMouseOverHandler(me,args);
                    return !args.cancel;
                });
                
            var onMouseOutHandler = this._events.getHandler("OnMouseOut");
            if(onMouseOutHandler)
                this._yuiControl.contextMouseOutEvent.subscribe(function(type, yuiArgs){ 
                    onMouseOutHandler(me,{element: yuiArgs[0], event: yuiArgs[1], yuiargs: yuiArgs});
                });
                
            var onContextTriggerHandler = this._events.getHandler("OnContextTrigger");
            if(onContextTriggerHandler)
                this._yuiControl.contextTriggerEvent.subscribe(function(type, yuiArgs){ 
                    onContextTriggerHandler(me,{element: yuiArgs[0]});
                });
        }
    },
    
    get_AttachToElements: function() {
        return this._attachToElements;
    },
    set_AttachToElements: function(attachToElements) {
        this._attachToElements = attachToElements;
    },
  
    get_HideDelay: function() {
        return this._hideDelay;
    },
    set_HideDelay: function(hideDelay) {
        this._hideDelay = hideDelay;
    },
  
    get_PreventOverlap: function() {
        return this._preventOverlap;
    },
    set_PreventOverlap: function(preventOverlap) {
        this._preventOverlap = preventOverlap;
    },
  
    get_ShowDelay: function() {
        return this._showDelay;
    },
    set_ShowDelay: function(showDelay) {
        this._showDelay = showDelay;
    },
  
    get_UseIFrame: function() {
        return this._useIFrame;
    },
    set_UseIFrame: function(useIFrame) {
        this._useIFramet = useIFrame;
    },
    
    get_AutoDismissDelay: function() {
        return this._autoDismissDelay;
    },
    set_AutoDismissDelay: function(autoDismissDelay) {
        this._autoDismissDelay = autoDismissDelay;
    },
    
    get_TooltipContainer: function() {
        return this._tooltipContainer;
    },
    set_TooltipContainer: function(tooltipContainer) {
        this._tooltipContainer = tooltipContainer;
    },
    
    get_TooltipContentContainer: function() {
        return this._tooltipContentContainer;
    },
    set_TooltipContentContainer: function(tooltipContentContainer) {
        this._tooltipContentContainer = tooltipContentContainer;
    },
        
    dispose: function() {
        $clearHandlers(this.get_element());
            
        //Add custom dispose actions here
        YUIAspNet.Tooltip.callBaseMethod(this, 'dispose');
    },
    
    _buildConfig: function() {
        var config = {};
        
        config.container = this.get_element().id;
        
        config.context = this.get_AttachToElements();
        config.text = $get(this.get_TooltipContentContainer()).innerHTML;
        
        if(this.get_Effect())
            config.effect = this._getEffect();
        
        if(this.get_ShowDelay() > 0)
            config.showdelay = this.get_ShowDelay();
        if(this.get_HideDelay() > 0)
            config.hidedelay = this.get_HideDelay();
            
        if(this.get_AutoDismissDelay() > 0)
            config.autodismissdelay = this.get_AutoDismissDelay();
            
        if(this.get_element().style.width)
            config.width = this.get_element().style.width;
            
        config.preventoverlap = this.get_PreventOverlap();
        config.iframe = this.get_UseIFrame();
                
        return config;
    },
    
    show: function(referenceElement) {
        this.get_YUIControl().doShow('mouseover', referenceElement || $get(this.get_AttachToElements()[0]));
    },
    
    hide: function() {
        this.get_YUIControl().hide();
    },

    add_OnMouseOver: function(handler) {
      this.get_events().addHandler("OnMouseOver", handler);
    },
    remove_OnMouseOver: function(handler) {
      this.get_events().removeHandler("OnMouseOver", handler);
    },

    add_OnMouseOut: function(handler) {
      this.get_events().addHandler("OnMouseOut", handler);
    },
    remove_OnMouseOut: function(handler) {
      this.get_events().removeHandler("OnMouseOut", handler);
    },

    add_OnContextTrigger: function(handler) {
      this.get_events().addHandler("OnContextTrigger", handler);
    },
    remove_OnContextTrigger: function(handler) {
      this.get_events().removeHandler("OnContextTrigger", handler);
    }
}

YUIAspNet.Tooltip.registerClass('YUIAspNet.Tooltip', YUIAspNet.BaseControl);
